$(function () {
//选择框
	$("#left li:first-child").addClass("active");
	var e;
	var imgsrc, menuId;
	var choseValue=0;
	//商品点击增加
	$(document).on('click', '.add', function icadd() {
		var choseValue=0;
		$(".subFly").show();
		var n = $(this).prev().text();
		var num = parseFloat(n);
		if(n == 0) {
			num = 1
		}
		$(".ad").prev().text(num);
		e = $(this).prev();
		var parent = $(this).parent();
		var name = parent.parent().children("h4").text();
		var names = parent.parent().children("h5").text();
//		alert(names);
		var imgobj = parent.parent().parent().children(".menu-img").children("img");
		imgsrc = imgobj[0].src;
		var menuIdobj = parent.parent().parent().children("span");
		menuId = menuIdobj.text();
		var price = parseFloat(parent.prev().children("b:nth-child(2)").text());
		$(".imgPhoto").attr('src', imgsrc);
		$(".pce").text(price);
		$(".pce2").text(price);
		$(".cainame").text(name);
		$(".Notes").text(names);
		
		$(this).siblings(".price").text(price);
		$(".mydd .price").html(price);
		$(".choseValue").text($(".subChose .m-active").text());
		var dataIcon = $(this).parent().parent().children("h4").attr("data-icon");
		addGuige(menuId);
	});
    
//选择规格之后的价格
		$(document).on('click', '.jump', function () {
		
		});
	function addGuige(itemNum) {
		for(var i = 0; i < returnData[0]["Categorys"].length; i++) {
			for(var a = 0; a < returnData[0]["Categorys"][i]["Menus"].length; a++) {
				var aa = returnData[0]["Categorys"][i]["Menus"][a].MenuId;
				if(aa == itemNum) {
					var MenuItemStr = '<dl class="subChose"><dt>Specifications</dt>';
					for(var c = 0; c < returnData[0]["Categorys"][i]["Menus"][a]["MenuItem"].length; c++) {
						MenuItemStr += '<dd title="' + returnData[0]["Categorys"][i]["Menus"][a]["MenuItem"][c].ItemPrice +'" tabindex="'+returnData[0]["Categorys"][i]["Menus"][a]["MenuItem"][c].ItemId+'">' + returnData[0]["Categorys"][i]["Menus"][a]["MenuItem"][c].ItemText +'</dd>';
					}
					MenuItemStr += '</dl>';
				}
			}
		}
		$("#subChose").html(MenuItemStr);
	}

	$(document).on('click', '.minus', function() {
		$('.shopcart-list,.up1').show();
	});

	//口味选择
	$(document).on('click', '.subChose dd', function() {
		$(this).addClass("m-active").siblings().removeClass("m-active");
		$(".choseValue").text($(".subChose .m-active").text());
		var pce = 0;
		choseValue=1;
		pce = parseFloat($(".subChose .m-active").attr("title"));
		var ItemId= $(".subChose .m-active").attr("tabindex");
		$(".pce").text(pce);
		$(".ItemId").text(ItemId);
	})

	//弹框 - 加
	$(".ad").click(function() {
		var n = parseFloat($(this).prev().text()) + 1;
		if(n == 0) {
			return;
		}
		$(this).prev().text(n);
		var danjia = $(this).next().text(); //获取单价
		var a = $("#totalpriceshow").html(); //获取当前所选总价
		var b = a * 1 + danjia * 1;
		$("#totalpriceshow").html(b); //计算当前所选总价
		var nm = $("#sumprice").html(); //获取数量
		$("#sumprice").html(nm * 1 + 1);
		
	});
	//弹框 - 减
	$(".ms").click(function() {
		var n = $(this).next().text();
		if(n > 1) {
			var num = parseFloat(n) - 1;
			$(this).next().text(num); //减1

			var danjia = $(this).nextAll(".price").text(); //获取单价
			var a = $("#totalpriceshow").html(); //获取当前所选总价
			var b = (a * 3 - danjia * 3) / 3;
			$("#totalpriceshow").html(b); //计算当前所选总价

			var nm = $("#totalcountshow").html(); //获取数量
			$("#totalcountshow").html(nm * 1 - 1);
		}

		//如果数量小于或等于0则隐藏减号和数量
		if(num <= 0) {
			$(this).next().css("display", "none");
			$(this).css("display", "none");
			jss(); //改变按钮样式
			return
		}
	});
	//点击遮罩，隐藏商品详情弹框
	$(".up").click(function() {
		$(".subFly").hide();
	});

var flag = false;

	$(".foot").click(function() {
		var Notes = "";
		if(choseValue == 0) {
			alert("Please select specifications");
		} else {
			var n = $('.ad').prev().text();
			var num = parseFloat(n) + 1;
			if(num == 0) {
				alert("3");
				return;
			}
			$('.ad').prev().text(num);
			var danjia = $(".pce").text();
			//var danjia = $('.ad').next().text();  //获取单价
			var a = $("#totalpriceshow").html(); //获取当前所选总价
			$("#totalpriceshow").html((a * 1 + danjia * 1).toFixed(2)); //计算当前所选总价
			var nm = $("#totalcountshow").html(); //获取数量
			$("#totalcountshow").html(nm * 1 + 1);
		
			$(".subFly").hide();
			var ms = e.text(num - 1);
			if(ms == 0) { //判断是否显示减号及数量
				e.css("display", "inline-block");
				e.prev().css("display", "inline-block")
			}
			var m = $(".cainame").text(); //当前商品名称
//			alert(m);
			var taste = $(".subChose .m-active").text();//当前商品规格
			var acount = 1; //当前商品数量
			var sum = parseFloat($(".subName dd p:nth-child(2) span:nth-child(2)").text()) * acount; //当前商品价格
			var price = parseFloat($(".subName dd p:nth-child(2) span:nth-child(2)").text());
			var ItemId = parseFloat($(".subName dd p:nth-child(2) span:nth-child(4)").text());
			if(Notes!=""){
				Notes = ""
			}
			Notes = $(".Notes").text();
			var dataIconN = $(this).parent().children(".subName").children("dd").children("p:first-child").attr("data-icon");
			
			
			//判断购物车里是否有商品，是否有相同规格的商品
			if($(".list-left ul li").length <= 0) {
				
				
				var addtr = '<li class="food" style="padding-top:0.1rem;">';
				addtr += '<div class="list_aaa">';
				addtr += '<div class="numitems" id="numitms"></div>';
				addtr += '<div><span class="accountName" data-icon="' + dataIconN + '">' + m + '</span><span class="taste">' + taste + '</span></div>';
				addtr += '<p class="fuhao">' + "$" + '</p>';
				addtr += '<div><span class="accountPrice">'  +sum.toFixed(2)  + '</span></div>';
				addtr += '<div class="btn">';
				addtr += '<button class="ms2" style="display: inline-block;"></button>';
				addtr += '<p class="li_acount">' + acount + '</p>';
				addtr += '<button class="ad2"></button>';
				addtr += '<i class="price" style="display: none;">' + price + '</i>';
				addtr += '<i class="menuId" style="display: none;">' + menuId + '</i>';
				addtr += '<i class="imgsrc">' + imgsrc + '</i>';
				addtr += '<i class="ItemId" style="display: none;">' + ItemId + '</i>';
				addtr += '<i class="Notess">' + Notes + '</i>';
				addtr += '</div>';
				addtr += '</div>';
				addtr += '</li>';
				$(".list-left ul").append(addtr);
				choseValue=0;
				return;
			} else {
				$(".list-left ul li").each(function() {
					if($(this).find("span.accountName").html() == m && $(this).find(".taste").html() == taste) {
						var count = parseInt($(this).find(".li_acount").html());
						count = count + 1;
						var aaa = parseFloat($(".subName dd p:nth-child(2) span:nth-child(2)").text()) * acount;
						var sum =aaa * count;

//						count += parseInt(n);
						$(this).find(".accountPrice").html(sum.toFixed(2));
						$(this).find(".li_acount").html(count); //对商品的数量进行重新赋值
						flag = true;
						choseValue=0;
//						alert(count);
						return false;
					} else {
						flag = false;
					}
				})
			}
			//如果为默认值也就是说里面没有此商品，所以添加此商品。
			if(flag == false) {
				var acount = 1;
				choseValue=0;
				var addtr = '<li class="food" style="padding-top:0.1rem;">';
				addtr += '<div class="list_aaa">';
				addtr += '<div class="numitems" id="numitms"></div>';;
				addtr += '<div><span class="accountName" data-icon="' + dataIconN + '">' + m + '</span><span class="taste">' + taste + '</span>'+ '</span><span class="fuhao">' + "$" + '</span></div>';                         
				addtr += '<div><span class="accountPrice">'  +sum.toFixed(2) + '</span></div>';
				addtr += '<div class="btn">';
				addtr += '<button class="ms2" style="display: inline-block;"></button>';
				addtr += '<p class="li_acount">' + acount + '</p>';
				addtr += '<button class="ad2"></button>';
				addtr += '<i class="price" style="display: none;">' + price + '</i>';
				addtr += '<i class="menuId" style="display: none;">' + menuId + '</i>';
				addtr += '<i class="imgsrc">' + imgsrc + '</i>';
				addtr += '<i class="ItemId" style="display: none;">' + ItemId + '</i>';
				addtr += '<i class="Notess" style="display: none;">' + Notes + '</i>';
				addtr += '</div>';
				addtr += '</div>';
				addtr += '</li>';
				$(".list-left ul").append(addtr);
				choseValue=0;
				return;
			}
				
		}

	});
	
	//购物车 - 加
	$(document).on('click', '.ad2', function() {

		var n = parseInt($(this).prev().text()) + 1;
		$(this).prev().text(n); //当前商品数量+1
		e.text(n); //赋值给商品列表的数量
		var p = parseFloat($(this).next().text()); //隐藏的价格
		$(this).parent().prev().children("span.accountPrice").text(((p * n)).toFixed(2)); //计算该商品规格的总价值
		$("#totalcountshow").text(parseFloat($("#totalcountshow").text()) + 1); //总数量＋1
		$("#totalpriceshow").text((parseFloat($("#totalpriceshow").text()) + p).toFixed(2)); //总价加上该商品价格
		
	});

	//购物车 - 减
	$('.list-left').on('click', '.ms2', function() {
		var a = parseFloat($(this).siblings(".price").text()); //当前商品单价
		var n = parseInt($(this).next().text()) - 1; //当前商品数量
		var s = parseFloat($("#totalpriceshow").text()); //总金额
		if(n == 0) {
			$(this).parent().parent().remove();
			$(".up1").hide();
			$("#btnselect").addClass("disable");
			//alert(e.css);
			e.css("display", "none");
			e.prev().css("display", "none")
			
			
		}
		$(this).next().text(n);
		e.text(n); //赋值给商品列表的数量
		$(this).parent().prev().children("span.accountPrice").text((a * n).toFixed(2)); 
		$("#totalcountshow").text(parseInt($("#totalcountshow").text()) - 1);
		$("#totalpriceshow").text((s - a).toFixed(2));
		if(parseFloat($("#totalcountshow").text()) == 0) {
			$(".shopcart-list").show();
			
		}		
	});


	//选项卡
	$(".con>div").hide();
	$(".con>div:eq(0)").show();

	$(".left-menu").on("click", "li", function() {
		$(this).addClass("active").siblings().removeClass("active");
		var n = $(".left-menu li").index(this);
		$(".left-menu li").index(this);
		$(".con>div").hide();
		$(".con>div:eq(" + n + ")").show();

	});

	$(".subFly").hide();
	$(".close").click(function() {
		$(".subFly").hide();
	});
	$(".footer>.left").click(function() {
		var content = $(".list-left>ul").html();
		if(content != "") {
			$(".shopcart-list.fold-transition").show();
			$(".up1").toggle();
		}
	});

	$(".up1").click(function() {
		$(".up1").hide();
		$(".shopcart-list.fold-transition").show();
	})

	//清空购物车
	$(".pg_12").click(function() {
		$(".list-left>ul").html("");
		$("#totalcountshow").text("0");
		$("#totalpriceshow").text("0.00");
		$(".minus").next().text("0");
		$(".minus").hide();
		$(".minus").next().hide();
		$(".shopcart-list").show();
		$(".up1").hide();
	});
});
//$(".food").click(function() {
//		
//		var lomg = document.getElementsByClassName('food');
//			var numitem = document.getElementById("numitms");
//			for(var i = 0; i < lomg.length+1; i++) {
////				$("#numites").text(lomg.length-i+".");
//              numitem.innerHTML = "<p>"+ i+"</p>";
//
//			}
//		
//		alert(lomg.length)
//	});